package com.ruoyi.bussiness.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonObject;
import com.ruoyi.bussiness.domain.BridgeNum;
import com.ruoyi.bussiness.domain.SysBusinessCheckList;
import com.ruoyi.bussiness.domain.SysListRelatedRead;
import com.ruoyi.bussiness.domain.SysListRelatedResource;
import com.ruoyi.bussiness.mapper.SysBusinessCheckListMapper;
import com.ruoyi.bussiness.mapper.SysListRelatedReadMapper;
import com.ruoyi.bussiness.mapper.SysListRelatedResourceMapper;
import com.ruoyi.bussiness.service.ISysBusinessCheckListService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 清单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@Service
public class SysBusinessCheckListServiceImpl implements ISysBusinessCheckListService
{
    @Autowired
    private SysBusinessCheckListMapper sysBusinessCheckListMapper;
    @Autowired
    private SysListRelatedReadMapper sysListRelatedReadMapper;
    @Autowired
    private SysListRelatedResourceMapper sysListRelatedResourceMapper;

    /**
     * 查询清单
     *
     * @param checkListId 清单主键
     * @return 清单
     */
    @Override
    public SysBusinessCheckList selectSysBusinessCheckListByCheckListId(Long checkListId)
    {
        return sysBusinessCheckListMapper.selectSysBusinessCheckListByCheckListId(checkListId);
    }

    /**
     * 查询清单列表
     *
     * @param sysBusinessCheckList 清单
     * @return 清单
     */
    @Override
    public List<SysBusinessCheckList> selectSysBusinessCheckListList(SysBusinessCheckList sysBusinessCheckList)
    {
        return sysBusinessCheckListMapper.selectSysBusinessCheckListList(sysBusinessCheckList);
    }

    /**
     * 新增清单
     *
     * @param sysBusinessCheckList 清单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessCheckList(SysBusinessCheckList sysBusinessCheckList)
    {
        sysBusinessCheckList.setCreateTime(DateUtils.getNowDate());
        sysBusinessCheckListMapper.insertSysBusinessCheckList(sysBusinessCheckList);
        sysBusinessCheckListMapper.updateListOrderNum(sysBusinessCheckList.getCheckListId());
        if(StringUtils.isNotEmpty(sysBusinessCheckList.getRelatedReadList())){
            List<SysListRelatedRead> relatedReadList = new ArrayList<>();
            for (Long dynamicId : sysBusinessCheckList.getRelatedReadList()) {
                SysListRelatedRead tempEntity = new SysListRelatedRead();
                tempEntity.setCheckListId(sysBusinessCheckList.getCheckListId());
                tempEntity.setDynamicId(dynamicId);
                relatedReadList.add(tempEntity);
            }
            sysListRelatedReadMapper.batchInsertRelatedRead(relatedReadList);
        }
        if(StringUtils.isNotEmpty(sysBusinessCheckList.getRelatedResourceList())){
            List<SysListRelatedResource> relatedResourceList = new ArrayList<>();
            for (SysListRelatedResource relatedResource : sysBusinessCheckList.getRelatedResourceList()) {
                relatedResource.setCheckListId(sysBusinessCheckList.getCheckListId());
                relatedResourceList.add(relatedResource);
            }
            sysListRelatedResourceMapper.batchInsertRelatedResource(relatedResourceList);
        }
        return 1;
    }

    /**
     * 修改清单
     *
     * @param sysBusinessCheckList 清单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysBusinessCheckList(SysBusinessCheckList sysBusinessCheckList)
    {
        sysBusinessCheckList.setUpdateTime(DateUtils.getNowDate());
        sysListRelatedReadMapper.deleteSysListRelatedReadByListId(sysBusinessCheckList.getCheckListId());
        sysListRelatedResourceMapper.deleteRelatedResourceByListId(sysBusinessCheckList.getCheckListId());
        if(StringUtils.isNotEmpty(sysBusinessCheckList.getRelatedReadList())){
            List<SysListRelatedRead> relatedReadList = new ArrayList<>();
            for (Long dynamicId : sysBusinessCheckList.getRelatedReadList()) {
                SysListRelatedRead tempEntity = new SysListRelatedRead();
                tempEntity.setCheckListId(sysBusinessCheckList.getCheckListId());
                tempEntity.setDynamicId(dynamicId);
                relatedReadList.add(tempEntity);
            }
            sysListRelatedReadMapper.batchInsertRelatedRead(relatedReadList);
        }
        if(StringUtils.isNotEmpty(sysBusinessCheckList.getRelatedResourceList())){
            List<SysListRelatedResource> relatedResourceList = new ArrayList<>();
            for (SysListRelatedResource relatedResource : sysBusinessCheckList.getRelatedResourceList()) {
                relatedResource.setCheckListId(sysBusinessCheckList.getCheckListId());
                relatedResourceList.add(relatedResource);
            }
            sysListRelatedResourceMapper.batchInsertRelatedResource(relatedResourceList);
        }
        return sysBusinessCheckListMapper.updateSysBusinessCheckList(sysBusinessCheckList);
    }

    /**
     * 批量删除清单
     *
     * @param checkListIds 需要删除的清单主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessCheckListByCheckListIds(Long[] checkListIds)
    {
        return sysBusinessCheckListMapper.deleteSysBusinessCheckListByCheckListIds(checkListIds);
    }

    /**
     * 删除清单信息
     *
     * @param checkListId 清单主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessCheckListByCheckListId(Long checkListId)
    {
        return sysBusinessCheckListMapper.deleteSysBusinessCheckListByCheckListId(checkListId);
    }

    @Override
    @Transactional
    public int withdrawSysBusinessCheckListByCheckListIds(Long[] checkListIds) {
        return sysBusinessCheckListMapper.withdrawBusinessCheckListByCheckListIds(checkListIds);
    }


    @Override
    @Transactional
    public AjaxResult moveUpOrDown(SysBusinessCheckList entity) {
        SysBusinessCheckList moveEntity = sysBusinessCheckListMapper.selectSysBusinessCheckListByCheckListId(entity.getCheckListId());
        if(moveEntity == null){
            return AjaxResult.error("清单不存在，不能上移下移");
        }
        SysBusinessCheckList moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessCheckListMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveEntity);
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessCheckListMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveEntity);
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public AjaxResult pinToTopOrEnd(SysBusinessCheckList entity) {
        SysBusinessCheckList moveEntity = sysBusinessCheckListMapper.selectSysBusinessCheckListByCheckListId(entity.getCheckListId());
        if(moveEntity == null){
            return AjaxResult.error("清单不存在，不能上移下移");
        }
        if("top".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsTop())){
                moveEntity.setIsTop("0");
                moveEntity.setIsBottom("0");
            }else{
                moveEntity.setIsTop("1");
                moveEntity.setIsBottom("0");
            }
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveEntity);
        }else if("end".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsBottom())){
                moveEntity.setIsBottom("0");
                moveEntity.setIsTop("0");
            }else{
                moveEntity.setIsBottom("1");
                moveEntity.setIsTop("0");
            }
            sysBusinessCheckListMapper.updateSysBusinessCheckList(moveEntity);
        }else{
            return AjaxResult.error("无效的操作方式");
        }
        return AjaxResult.success("置顶置底成功",1);
    }

    @Override
    public AjaxResult getRelateReadList(SysBusinessCheckList entity) {
        SysListRelatedRead queryEntity = new SysListRelatedRead();
        queryEntity.setCheckListId(entity.getCheckListId());
        List<SysListRelatedRead> relatedReadList = sysListRelatedReadMapper.selectSysListRelatedReadList(queryEntity);
        return AjaxResult.success("获取成功",relatedReadList);
    }

    @Override
    public String getBridgeNum(String bridgeType) {
        String maxCheckListId = sysBusinessCheckListMapper.getMaxCheckListID(bridgeType);
        String coverListID = String.format("%04d", Integer.parseInt(maxCheckListId));
        return coverListID;
    }

    @Override
    public AjaxResult getBridgeBigData() {
        //桥梁总数
        int bridgeCount = sysBusinessCheckListMapper.getCountBridge();
        //古代桥梁
        int oldBridgeCount = sysBusinessCheckListMapper.getCountOldBridge();
        //现代桥梁
        int xdBridgeCount = sysBusinessCheckListMapper.getCountXdBridge();
        //每个区的桥梁数量
        List<BridgeNum> bridgeNumList = sysBusinessCheckListMapper.getNumGroupByCounty();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bridgeCount",bridgeCount);
        jsonObject.put("oldBridgeCount",oldBridgeCount);
        jsonObject.put("xdBridgeCount",xdBridgeCount);
        jsonObject.put("bridgeGroup",bridgeNumList);
        return AjaxResult.success(jsonObject);
    }

    @Override
    public List<SysListRelatedResource> getRelateResourceListByType(SysListRelatedResource sysListRelatedResource) {
        return sysListRelatedResourceMapper.getRelatedResourceByType(sysListRelatedResource);
    }


    @Override
    public AjaxResult getRelateResourceList(SysBusinessCheckList entity) {
        SysListRelatedResource queryEntity = new SysListRelatedResource();
        queryEntity.setCheckListId(entity.getCheckListId());
        List<SysListRelatedResource> relatedReadList = sysListRelatedResourceMapper.selectSysListRelatedResourceList(queryEntity);
        return AjaxResult.success("获取成功",relatedReadList);
    }

}
